package com.linkstec.util;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.ResourceBundle;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

/**
 * 
 * 默认ZTest配置文件
 * 
 * @author LYL
 * 
 */
public class ConfigUtil {

	private static Logger logger = Logger.getLogger(ConfigUtil.class);
	private static Map<String, String> configMsg = null;

	/**
	 * JDBC 批处理插入最大记录数
	 */
	private final static int DEFAULT_MAX_COMMIT_RECORDS = 1000;

	static {
		configMsg = getConfigMap("ZTest");
		logger.info(configMsg.toString());
	}

	public static Map<String, String> getConfigMap(String configFilePath) {
		if (StringUtils.isBlank(configFilePath)) {
			return null;
		}
		int dotIndex = configFilePath.lastIndexOf(",");
		if (dotIndex > 0) {
			configFilePath = new String(configFilePath.substring(0, dotIndex));
		}
		ResourceBundle rb = ResourceBundle.getBundle(configFilePath);
		Map<String, String> result = new HashMap<String, String>();
		if (null != rb) {
			Enumeration<String> configEnum = rb.getKeys();
			while (configEnum.hasMoreElements()) {
				String key = configEnum.nextElement();
				result.put(key, rb.getString(key));
			}
		}
		return result;
	}

	/**
	 * 获取appconfig中配置信息 不带默认值
	 * 
	 * @param key
	 * @return
	 */
	public static String getProperty(String key) {
		return getProperty(key, null);
	}

	/**
	 * 获取appconfig中配置信息 带默认值
	 * 
	 * @param key
	 * @param defaultVal
	 * @return
	 */
	public static String getProperty(String key, String defaultVal) {
		return configMsg.get(key) == null ? defaultVal : configMsg.get(key);
	}
	/**
	 * @return JDBC 批处理插入最大记录数
	 */
	public static int getMaxCommitRecords() {
		String maxCommitRecords = getProperty("max_commit_records");
		if (maxCommitRecords != null) {
			return Integer.parseInt(maxCommitRecords);
		} else {
			return DEFAULT_MAX_COMMIT_RECORDS;
		}
	}

}
